package com.yunding.interceptor;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 郭思良
 * @create 2020-08-15 19:36
 */
// TODO: 2020/8/30  

@Slf4j
public class MyRoleFilter {
//     extends RolesAuthorizationFilter
//
//    public <T> T getBean(Class<T> clazz, HttpServletRequest request){
//        WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
//        return applicationContext.getBean(clazz);
//    }
//
//    @Override
//    public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
//        return false;
//    }

//    @Override
//    protected boolean onAccessDenied(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
//
//        HttpServletRequest req = (HttpServletRequest) request;
//        HttpServletResponse res = (HttpServletResponse) response;
//        // TODO: 2020/8/16 如何可以在这里获取到projectId 1.前端将projectId存入cookie，2.前端存入headers
//        Subject subject = SecurityUtils.getSubject();
//        ActivityUser currentUser = (ActivityUser) subject.getPrincipal();
//        if (currentUser == null){
//            throw new ServiceException(ResultStatuesEnums.NOT_LOGIN);
//        }
//        //进行拦截
//        //如果是系统管理员则放行
//        String[] roles = (String[]) mappedValue;
//        if (currentUser.getUser().getUserIdentify() == 2){
//            return true;
//        }else{
//            SysUserProjectRoleMapper sysUserProjectRoleMapper = getBean(SysUserProjectRoleMapper.class,req);
//            String roleId = "";
//            // 获取 projectId，查询数据库获得roleId
//            String projectId = req.getHeader(SystemConstants.PROJECT_ID);
//            if (projectId == null){
//                throw new ServiceException("查询不到用户在该项目中的角色");
//            }
//            SysUserProjectRole sysUserProjectRole = sysUserProjectRoleMapper.selectOne(new QueryWrapper<SysUserProjectRole>().eq(SysUserProjectRole.COL_USER_ID, currentUser.getUser().getUserId())
//                    .eq(SysUserProjectRole.COL_PROJECT_ID, projectId));
//            if (sysUserProjectRole != null){
//                roleId = sysUserProjectRole.getRoleId();
//            }
//            //如果是普通用户则核对角色信息
//            for (String role : roles){
//                if (role.equals(roleId)){
//                    return true;
//                }
//            }
//        }
//        // 设置响应头部信息
//        res.setCharacterEncoding("UTF-8");
//        res.setContentType("application/json");
//        //返回消息
//        res.getWriter().print(MapperUtil.obj2json(BaseResult.notOk(ResultStatuesEnums.BAD_ROLE)));
//
//        return false;
//    }
}
